﻿#include <QFile>
#include <QMessageBox>
#include "CRobTaskMngDialog.h"
#include "CHeader.h"
#include "CSqlOper.h"
#include "CRobTaskOper.h"
#include "CLogOper.h"

CRobTaskMngDialog::CRobTaskMngDialog(QString sSceneGuid, CRBaseData* BasicDataBase, QWidget *parent /*= nullptr*/)
	: m_sGuid(sSceneGuid),m_pBasicData(BasicDataBase)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setStyleSheetFile();
	initUI();
	initData();
	initConnect();
	slotPushButtonQueryClicked();
}

CRobTaskMngDialog::~CRobTaskMngDialog()
{}

void CRobTaskMngDialog::setStyleSheetFile()
{
	//设置对话框标题
	this->setWindowTitle(A2T("任务管理"));
	QString sQss = setUiStyle("card");
	QString sMyqss = styleSheet();
	QString sMerged = sQss + "\n" + sMyqss; // 后面的会覆盖前面的相同规则	
	setStyleSheet(sMerged);
}

void CRobTaskMngDialog::initConnect()
{
	connect(ui.widget->ui.tableView, &QTableView::doubleClicked, this, &CRobTaskMngDialog::slotPushButtonModifyClicked);
	connect(ui.pushButtonQuery, &QPushButton::clicked, this, &CRobTaskMngDialog::slotPushButtonQueryClicked);
	connect(ui.pushButtonAdd, &QPushButton::clicked, this, &CRobTaskMngDialog::slotPushButtonAddClicked);
	connect(ui.pushButtonModify, &QPushButton::clicked, this, &CRobTaskMngDialog::slotPushButtonModifyClicked);
	connect(ui.pushButtonDelete, &QPushButton::clicked, this, &CRobTaskMngDialog::slotPushButtonDeleteClicked);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CRobTaskMngDialog::slotClose);
	connect(ui.pushButtonInstantly, &QPushButton::clicked, this, &CRobTaskMngDialog::slotPusButtonInstantlyClicked);
}

void CRobTaskMngDialog::initUI()
{
	QStringList headerListAction;		//水平表头
	headerListAction << A2T("序号") << A2T("任务编号") << A2T("任务名称") << A2T("模版名称") << A2T("开始生效时间") << A2T("任务类型") <<
		A2T("任务周期类型") << A2T("任务创建时间") << A2T("连续测温") << A2T("连续测声");//wxy++20240430 两个连续测选项

	QList<quint16> listColumnWidth;		//列宽
	listColumnWidth << 150 << 350 << 350 << 150 << 250 << 150 << 150 << 150 << 150 << 150;
	QList<quint16> listColumnHidden;	//隐藏列
	listColumnHidden.append(1);
	//设置列数
	ui.widget->m_pModel->setColumnCount(headerListAction.count());
	//设置表头
	ui.widget->m_pModel->setHorizontalHeaderLabels(headerListAction);
	ui.widget->ui.tableView->setModel(ui.widget->m_pModel);
	//设置列宽
	for (int i = 0; i < listColumnWidth.count(); i++)
	{
		ui.widget->ui.tableView->setColumnWidth(i, listColumnWidth.at(i));
	}
	//隐藏列
	if (0 < listColumnHidden.count())
	{
		for (quint16 i = 0; i < listColumnHidden.count(); i++)
		{
			ui.widget->ui.tableView->hideColumn(listColumnHidden.at(i));
		}
	}
}

void CRobTaskMngDialog::initData()
{
	ui.comboBoxTaskType->addItem(A2T("*"));
	ui.comboBoxTaskType->addItem(A2T("定时"));
	ui.comboBoxTaskType->addItem(A2T("周期"));
	ui.comboBoxTaskType->addItem(A2T("信号触发任务A侧"));
	ui.comboBoxTaskType->addItem(A2T("信号触发任务B侧"));
	m_mapTaskType.insert(-1, A2T("*"));
	m_mapTaskType.insert(0, A2T("定时性任务"));
	m_mapTaskType.insert(1, A2T("周期性任务"));
	m_mapTaskType.insert(2, A2T("立即执行任务"));
	m_mapTaskType.insert(3, A2T("信号触发任务A侧"));
	m_mapTaskType.insert(4, A2T("信号触发任务B侧"));
	ui.comboBoxPeriodType->addItem(A2T("*"));
	ui.comboBoxPeriodType->addItem(A2T("每十二小时"));
	ui.comboBoxPeriodType->addItem(A2T("每六小时"));
	ui.comboBoxPeriodType->addItem(A2T("每四小时"));
	ui.comboBoxPeriodType->addItem(A2T("每两小时"));
	ui.comboBoxPeriodType->addItem(A2T("每小时"));
	ui.comboBoxPeriodType->addItem(A2T("每半小时"));
	m_mapPeriodType.insert(-1, A2T("*"));
	m_mapPeriodType.insert(0, A2T("每十二小时"));
	m_mapPeriodType.insert(1, A2T("每六小时"));
	m_mapPeriodType.insert(2, A2T("每四小时"));
	m_mapPeriodType.insert(3, A2T("每两小时"));
	m_mapPeriodType.insert(4, A2T("每小时"));
	m_mapPeriodType.insert(5, A2T("每半小时"));

	//wxy++20240430
	m_mapContIrTemp.insert(0, A2T("关闭"));
	m_mapContIrTemp.insert(1, A2T("开启"));
	m_mapContSoundDet.insert(0, A2T("关闭"));
	m_mapContSoundDet.insert(1, A2T("开启"));

	//遍历模版，加载模版到map中
	QMap<QString, CTemplateInfo*> mapTemplateInfo;
	QString sErrMsg = "";
	selectTemplateBySceneID(m_sGuid, mapTemplateInfo, sErrMsg);
	for (QMap<QString, CTemplateInfo*>::iterator itor = mapTemplateInfo.begin(); itor != mapTemplateInfo.end(); itor++)
	{
		CTemplateInfo* pTemplateInfo = itor.value();
		m_mapTemplate.insert(pTemplateInfo->m_sTemplateFixedID, pTemplateInfo->m_sTemplateName);
	}

	m_mapTemplate.insert("", A2T("*"));
	for (QMap<QString, QString>::iterator itor = m_mapTemplate.begin(); itor != m_mapTemplate.end(); itor++)
	{
		ui.comboBoxTamplateName->addItem(itor.value());
	}
}

void CRobTaskMngDialog::slotPushButtonQueryClicked()
{
	//首先清空列表
	ui.widget->m_pModel->removeRows(0, ui.widget->m_pModel->rowCount());

	CTaskInfo taskInfo;
	taskInfo.m_sGuid=m_sGuid;
	taskInfo.m_sTemplateFixedID=m_mapTemplate.key(ui.comboBoxTamplateName->currentText());
	taskInfo.m_nTaskType = m_mapTaskType.key(ui.comboBoxTaskType->currentText());
	taskInfo.m_nPeriodType = m_mapPeriodType.key(ui.comboBoxPeriodType->currentText());

	//执行Sql查询语句
	QVector<QSqlRecord> vecRecord;
	QString sErrMsg = "";
	selectTaskInfo(taskInfo, vecRecord, sErrMsg);

	//刷新状态
	int nCount = vecRecord.size();
	ui.widget->UpdateStatus(nCount);
	int nNo = 1;
	if (nCount > 0)
	{
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QMap<QString, QString> mapTaskInfo;
			QList<QStandardItem*> item;
			item.append(new QStandardItem(QString::number(nNo)));//序号
			item.append(new QStandardItem(vecRecord[i].value("GUID").toString()));//任务编号
			item.append(new QStandardItem(vecRecord[i].value("TASK_NAME").toString()));//任务名称
			item.append(new QStandardItem(m_mapTemplate.value(vecRecord[i].value("TEMPLATE_FIXED_ID").toString())));//模版名称
			if (vecRecord[i].value("TASK_TYPE").toInt() == 0)//定时
			{
				item.append(new QStandardItem(vecRecord[i].value("SCHEDULED_TIME").toDateTime().toString("hh:mm:ss")));//定时时间
			}
			else if (vecRecord[i].value("TASK_TYPE").toInt() == 1)//周期
			{
				item.append(new QStandardItem("/"));
			}
			else if (vecRecord[i].value("TASK_TYPE").toInt() == 2)//立即执行
			{
				//立即执行类型的不显示，直接continue
				continue;
				//item.append(new QStandardItem(vecRecord[i].value("SCHEDULED_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss")));//执行时间
			}
			else if (vecRecord[i].value("TASK_TYPE").toInt() == 3)//信号触发任务A侧
			{
				item.append(new QStandardItem("/"));
			}
			else if (vecRecord[i].value("TASK_TYPE").toInt() == 4)//信号触发任务B侧
			{
				item.append(new QStandardItem("/"));
			}
			//item.append(new QStandardItem(vecRecord[i].value("SCHEDULED_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss")));//定时时间
			item.append(new QStandardItem(m_mapTaskType.value(vecRecord[i].value("TASK_TYPE").toInt())));//任务类型
			item.append(new QStandardItem(m_mapPeriodType.value(vecRecord[i].value("PERIOD_TYPE").toInt())));//任务周期类型
			item.append(new QStandardItem(vecRecord[i].value("CREATE_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss")));//任务创建时间
			//wxy++20240430
			item.append(new QStandardItem(m_mapContIrTemp.value(vecRecord[i].value("CONT_IR_TEMP").toInt())));//是否开启连续红外测温(0:关闭 1:打开)
			item.append(new QStandardItem(m_mapContSoundDet.value(vecRecord[i].value("CONT_SOUND_DET").toInt())));//是否开启连续噪声检测(0:关闭 1:打开)
			nNo++;
			//设置居中显示
			for (int nRow = 0; nRow < item.count(); nRow++)
			{
				item.at(nRow)->setTextAlignment(Qt::AlignCenter);
				item.at(nRow)->setForeground(QBrush(QColor(255, 255, 255)));
			}
			ui.widget->m_pModel->insertRow(ui.widget->m_pModel->rowCount(), item);
		}
	}
}

void CRobTaskMngDialog::slotPushButtonAddClicked()
{
	CRobTaskOper oper;
	oper.m_nType = 0;//表示增加
	oper.m_sGuid = m_sGuid;
	oper.setData(m_mapTaskType, m_mapPeriodType, m_mapTemplate);
	oper.setTitle(A2T("添加任务"));
	oper.exec();
	slotPushButtonQueryClicked();
}

void CRobTaskMngDialog::slotPushButtonModifyClicked()
{
	int nRow = ui.widget->ui.tableView->currentIndex().row();
	if (nRow < 0)
	{
		messageBox(0, A2T("请选择一个记录在进行操作"));
		return;
	}

	CRobTaskOper oper;
	oper.m_nType = 1;//表示修改
	oper.m_sGuid = m_sGuid;
	oper.m_sTaskGuid= ui.widget->m_pModel->item(nRow, 1)->text();
	oper.setData(m_mapTaskType, m_mapPeriodType, m_mapTemplate);
	QString sTaskName = ui.widget->m_pModel->item(nRow, 2)->text();
	QString sTemplateName= ui.widget->m_pModel->item(nRow, 3)->text();
	QString sTime= ui.widget->m_pModel->item(nRow, 4)->text();
	int nTaskType = m_mapTaskType.key(ui.widget->m_pModel->item(nRow, 5)->text());
	int nPeriodType= m_mapPeriodType.key(ui.widget->m_pModel->item(nRow, 6)->text());
	//wxy++20240430
	int nContIrTemp = m_mapContIrTemp.key(ui.widget->m_pModel->item(nRow, 8)->text());
	int nContSoundDet = m_mapContSoundDet.key(ui.widget->m_pModel->item(nRow, 9)->text());

	oper.setUI(sTaskName, sTemplateName, sTime, nTaskType, nPeriodType, nContIrTemp, nContSoundDet);
	oper.exec();
	slotPushButtonQueryClicked();
}

void CRobTaskMngDialog::slotPushButtonDeleteClicked()
{
	int nRow = ui.widget->ui.tableView->currentIndex().row();
	if (nRow < 0)
	{
		messageBox(0, A2T("请选择一个记录再进行操作"));
		return;
	}
	
	int nRet=messageBox(3, A2T("是否确定删除此任务"));
	if (nRet == QMessageBox::No || QMessageBox::Close == nRet)
	{
		return;
	}
	QString sGuid = ui.widget->m_pModel->item(nRow, 1)->text();
	QString sName = ui.widget->m_pModel->item(nRow, 2)->text();
	QString sErrMsg = "";
	//首先将规则表中该任务占的位子清空
	deleteTaskRule(m_sGuid, sGuid, sErrMsg);
	deleteTaskInfoByTaskID(sGuid, sErrMsg);
	SExecLog sLog;
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = sName;
	/*sLog.m_sDescriptionl = A2T("%1删除了%2名为%3的任务")
		.arg(CPublicData::instance().m_userInfo.sUserName)
		.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
		.arg(sName);*/
	sLog.m_sDevName = m_sGuid;
	sLog.m_sFuncName = A2T("任务");
	CLogOper::instance().addMsg(sLog);
	messageBox(0, A2T("操作成功"));
	slotPushButtonQueryClicked();
}

void CRobTaskMngDialog::slotClose()
{
	this->close();
}

void CRobTaskMngDialog::slotPusButtonInstantlyClicked()
{
	int nRow = ui.widget->ui.tableView->currentIndex().row();
	if (nRow < 0)
	{
		messageBox(0, A2T("请选择一个记录再进行操作"));
		return;
	}

	QString sTemplateName = ui.widget->m_pModel->item(nRow, 3)->text();
	QString sTaskName = ui.widget->m_pModel->item(nRow, 2)->text();
	QString sTemplateGuid = m_mapTemplate.key(sTemplateName);
	int nContIrTemp = m_mapContIrTemp.key(ui.widget->m_pModel->item(nRow, 8)->text());
	int nContSoundDet = m_mapContSoundDet.key(ui.widget->m_pModel->item(nRow, 9)->text());
	QString sGuid = createGuid();
	CTaskInfo taskInfo;
	taskInfo.m_sTaskGuid = sGuid;//任務Guid
	taskInfo.m_nLatestRecord = 1;//是否最新记录
	taskInfo.m_nTaskType = 2;//立即执行
	taskInfo.m_nPeriodType = -1;
	taskInfo.m_sScheduledTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
	taskInfo.m_sGuid = m_sGuid;
	taskInfo.m_sTaskName = sTaskName;
	taskInfo.m_sTemplateFixedID = sTemplateGuid;
	taskInfo.m_nContIrTemp = nContIrTemp;//是否开启连续红外测温(0:关闭 1:打开)
	taskInfo.m_nContSoundDet = nContSoundDet;//是否开启连续噪声检测(0:关闭 1:打开)

	QString sErrMsg = "";
	addTaskInfo(taskInfo, sErrMsg);
	SExecLog sLog;
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = sTaskName;
	sLog.m_sDevName =m_sGuid;
	sLog.m_sDevName = A2T("任务");
	CLogOper::instance().addMsg(sLog);
}
